<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @copyright   Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<div class="fieldset-wrapper">
    <div class="fieldset-wrapper-title">
        <span class="title"><?php echo __('Items to Ship') ?></span>
    </div>
    <div class="grid">
        <table cellspacing="0" class="data order-tables">
            <thead>
                <tr class="headings">
                    <th class="col-product"><span><?php echo __('Product') ?></span></th>
                    <th class="col-ordered-qty"><span><?php echo __('Qty') ?></span></th>
                    <th class="col-qty<?php if ($this->isShipmentRegular()): ?> last<?php endif; ?>">
                        <span><?php echo __('Qty to Ship') ?></span>
                    </th>
                    <?php if (!$this->canShipPartiallyItem()): ?>
                    <th class="col-ship last"><span><?php echo __('Ship') ?></span></th>
                    <?php endif; ?>
                </tr>
            </thead>
            <?php $_items = $this->getShipment()->getAllItems() ?>
            <?php $_i=0;foreach ($_items as $_item): if ($_item->getOrderItem()->getIsVirtual() || $_item->getOrderItem()->getParentItem()): continue; endif; $_i++ ?>
                <tbody class="<?php echo $_i%2?'odd':'even' ?>">
                    <?php echo $this->getItemHtml($_item) ?>
                    <?php echo $this->getItemExtraInfoHtml($_item->getOrderItem()) ?>
                </tbody>
            <?php endforeach; ?>
        </table>
    </div>
</div>
<div class="clearfix">
    <div class="order-comments-history">
        <div class="fieldset-wrapper">
            <div class="fieldset-wrapper-title"><span class="title"><?php echo __('Shipment Comments') ?></span></div>
            <fieldset>
                <div id="order-history_form">
                    <label class="normal" for="shipment_comment_text"><?php echo __('Shipment Comments') ?></label>
                    <textarea id="shipment_comment_text" name="shipment[comment_text]" rows="3" cols="5"><?php echo $this->getShipment()->getCommentText(); ?></textarea>
                </div>
            </fieldset>
        </div>
    </div>
    <div class="order-totals">
        <div class="fieldset-wrapper">
            <?php if ($this->canCreateShippingLabel()): ?>
                <div class="field choice field-create">
                    <input id="create_shipping_label" name="shipment[create_shipping_label]" value="1" type="checkbox"
                           onclick="toggleCreateLabelCheckbox();"/>
                    <label class="normal" for="create_shipping_label"><?php echo __('Create Shipping Label') ?></label>
                </div>
            <?php endif ?>
            <div class="field choice field-append">
                <input id="notify_customer" name="shipment[comment_customer_notify]" value="1" type="checkbox"/>
                <label class="normal" for="notify_customer"><?php echo __('Append Comments') ?></label>
            </div>
            <?php if ($this->canSendShipmentEmail()): ?>
                <div class="field choice field-email">
                    <input id="send_email" name="shipment[send_email]" value="1" type="checkbox"/>
                    <label class="normal" for="send_email"><?php echo __('Email Copy of Shipment') ?></label>
                </div>
            <?php endif; ?>
            <div class="actions">
                <?php echo $this->getChildHtml('submit_button') ?>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
require([
    "jquery",
    "prototype"
], function(jQuery){

//<![CDATA[
var sendEmailCheckbox = $('send_email');
if (sendEmailCheckbox) {
    var notifyCustomerCheckbox = $('notify_customer');
    var shipmentCommentText = $('shipment_comment_text');
    Event.observe(sendEmailCheckbox, 'change', bindSendEmail);
    bindSendEmail();
}
function bindSendEmail() {
    if (sendEmailCheckbox.checked == true) {
        notifyCustomerCheckbox.disabled = false;
    }
    else {
        notifyCustomerCheckbox.disabled = true;
    }
}
window.toggleCreateLabelCheckbox = function() {
    var checkbox = $('create_shipping_label');
    var submitButton = checkbox.up('.order-totals').select('.submit-button span')[0];
    if (checkbox.checked) {
        submitButton.innerText += '...';
    } else {
        submitButton.innerText = submitButton.innerText.replace(/\.\.\.$/, '');
    }
}
window.submitShipment = function(btn) {
    if (!validQtyItems()) {
        alert('<?php echo __('Invalid value(s) for Qty to Ship') ?>');
        return;
    }
    var checkbox = $(btn).up('.order-totals').select('#create_shipping_label')[0];
    if (checkbox && checkbox.checked) {
        packaging.showWindow();
    } else {
        disableElements('submit-button');
        // Temporary solution will be replaced after refactoring order functionality
        jQuery('#edit_form').on('invalid-form.validate', function() {
            enableElements('submit-button');
            jQuery('#edit_form').off('invalid-form.validate');
        });
        jQuery('#edit_form').triggerHandler('save');
    }
}
window.validQtyItems = function() {
    var valid = true;
    $$('.qty-item').each(function(item) {
        var val = parseFloat(item.value);
        if (isNaN(val) || val < 0) {
            valid = false;
        }
    });
    return valid;
}

window.bindSendEmail = bindSendEmail;

window.shipmentCommentText = shipmentCommentText;
window.notifyCustomerCheckbox = notifyCustomerCheckbox;
window.sendEmailCheckbox = sendEmailCheckbox;
//]]>

});
</script>
